﻿using System.Drawing;
using System.Windows.Forms;
using MovieBrowser.Model;
using MovieBrowser.Util;

namespace MovieBrowser.Presentation.Forms.Db
{
    public partial class KeywordsForm : Form
    {
        private readonly MovieDbEntities _db;
        public KeywordsForm(MovieDbEntities context)
        {
            _db = context;
            InitializeComponent();

            dataListView1.DataSource = _db.Keywords;
        }



        private void DataListView1SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (dataListView1.SelectedObject != null)
            {
                ratingStar1.Rating = ((Keyword)dataListView1.SelectedObject).Rated;
            }
        }

        private void SearchTextBox1SearchStarted(object sender, System.EventArgs e)
        {
            ComponentUtility.TimedFilter(dataListView1, searchTextBox1.Text);
        }

        private void SearchTextBox1SearchCancelled(object sender, System.EventArgs e)
        {
            ComponentUtility.TimedFilter(dataListView1, "");
        }

        private void RatingStar1RatingValueChanged(object sender, NymphsLib.RatingControl.RatingChangedEventArgs e)
        {
            foreach (Keyword keyword in dataListView1.SelectedObjects)
            {
                keyword.Rated = (int)ratingStar1.Rating;
                //Keyword keyword1 = keyword;
                //var k = _db.Keywords.Where(o => o.Id == keyword1.Id).FirstOrDefault().Rated = (int)ratingStar1.Rating;
            }
            _db.SaveChanges();
        }

        private void DataListView1FormatRow(object sender, BrightIdeasSoftware.FormatRowEventArgs e)
        {
            var w = (Keyword)e.Model;
            e.Item.ForeColor = ComponentUtility.HardnessForeground[w.Rated];
            e.Item.BackColor = ComponentUtility.HardnessBackground[w.Rated];
            e.Item.Font = w.Rated > 0 ? new Font(dataListView1.Font, FontStyle.Bold) : new Font(dataListView1.Font, FontStyle.Regular);

        }


    }
}
